package yto.freemarker.springbootstudy.jdkdynamic;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Proxy;

/**
 * jdk动态代理的案例
 *
 * @author dongxing
 * @create 2018-07-12 8:49
 **/
public class TestDynamic {


    public static void main(String[] args) {
        UserService userService = new UserServiceImpl();

        UserService o = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), new UserServiceInvocationHandler(userService));

        o.insert(null);

    }


    @Test
    public void test() {

        UserService userService = new UserServiceImpl();
        UserService o = (UserService) Proxy.newProxyInstance(userService.getClass().getClassLoader(), userService.getClass().getInterfaces(), (x, y, z) -> {
            System.out.println("前置增强！");
            Object invoke = y.invoke(userService, z);
            System.out.println("后置增强！");
            return invoke;
        });
        o.insert(null);
    }
}
